﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YGREKDOC : DocumentBase
    {
        #region-------------Properties-----------------------
        public YGREKDOCData m_Data;
        public string PrnType = "OpenExcel";
        ITXEXRPT ITXEXRPT;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        } 
 
        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwMaster=======
        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if ((DataRowView)vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        #endregion

        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtAUFLFD.Text = value;
            }
        }
        #endregion
        public YGREKDOC()
        {
            InitializeComponent();
            this.Name = "采购单管理";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            //IsSetUp = true;
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                base.SetUp(dicParamater);
                InitializeComponent();
            }
            m_Data = new YGREKDOCData();
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";
            txtUserID.Text = m_Data.app.User;
            txtPassword.Password = m_Data.app.Pwd;
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password); 
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            expLog.IsExpanded = false;
            btnQuery.IsEnabled = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                m_Data = new YGREKDOCData();
                SetEnv.Merge(m_Data.LoadEnv());
                vwLIEFDAT.View.CurrentChanged += vwLIEFDAT_CurrentChanged;
                KRS = "14";
                LFD = "000001";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwLIEFDAT_CurrentChanged(object sender, EventArgs e)
        {
            if (dwLIEFDAT == null)
                return;

            DataSet Result = new DataSet();
            Result = m_Data.GetOrderBySupplier(dwLIEFDAT);
            SetData.EKKOPF.Clear();
            SetData.Merge(Result, false, MissingSchemaAction.Add);
        }


        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            
            try
            {
                Result = m_Data.BuildData("00", KRS, LFD); 
                SetData.Clear();
                SetData.Merge(Result, false, MissingSchemaAction.Add);
            }
            catch (Exception ex)
            {

            }
        } 
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        { 
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv); 
            ITXEXRPT.DocType = app.company + "EKDOC";
            ITXEXRPT.strExcelFile = app.company + @"\YGREK.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        } 
        private void txtAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }
    }
}
